<?php
namespace WeChatPay\Request;

use WeChatPay\Formatter;

/**
 * 签名
 * 传入对应的参数输出签名
 * Class WeChatPayTradeSignRequest
 * @package WeChatPay\Request
 */
class WeChatPayTradeServiceSignRequest extends AbstractBaseRequest
{
    protected $apiUri = "";

    /**
     * 请求方式
     * @var string
     */
    public $requestType = '';

    /**
     * 时间戳
     * @var string
     */
    protected $timestamp = "";

    /**
     * 随机字符串
     * @var string
     */
    protected $nonce = "";

    /**
     * 请求体内容JSON
     * @var string
     */
    protected $body = "";

    /**
     * @return string
     */
    public function getApiUri(): string
    {
        return $this->apiUri;
    }

    /**
     * @param string $apiUri
     */
    public function setApiUri(string $apiUri): void
    {
        $this->apiUri = $apiUri;
    }

    /**
     * @return string
     */
    public function getRequestType(): string
    {
        return strtoupper($this->requestType ?: "GET");
    }

    /**
     * @param string $requestType
     */
    public function setRequestType(string $requestType): void
    {
        $this->requestType = $requestType;
    }

    /**
     * 获取时间戳
     * @return string
     */
    public function getTimestamp(): string
    {
        $this->timestamp = (string) ($this->timestamp ?: time());
        return $this->timestamp;
    }

    /**
     * 设置时间戳
     * @param string $timestamp
     */
    public function setTimestamp(string $timestamp): void
    {
        $this->timestamp = $timestamp;
    }

    /**
     * 获取随机字符串
     * @return string
     */
    public function getNonce(): string
    {
        $this->nonce = $this->nonce ?: Formatter::nonce();
        return $this->nonce;
    }

    /**
     * 设置随机字符串
     * @param string $nonce
     */
    public function setNonce(string $nonce): void
    {
        $this->nonce = $nonce;
    }

    /**
     * 设置加密的
     * @param $body
     */
    public function setBody($body) {
        $this->body = $body;
    }

    /**
     * 获取BODY内容
     * @return false|string
     */
    public function getBody() {
        return is_string($this->body) ? $this->body : json_encode($this->body, JSON_UNESCAPED_UNICODE);
    }
}